package com.example.entity;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;

public record ResultBean<T>(int code, T data, String message) {
    public static <T> ResultBean<T> success(T data) {
        return new ResultBean<>(200, data, "请求成功");
    }

    public static <T> ResultBean<T> success() {
        return success(null);
    }

    public static <T> ResultBean<T> fail(T data) {
        return new ResultBean<>(400, data, "请求失败");
    }
    public static <T> ResultBean<T> fail() {
        return fail(null);
    }


    public String toJson() {
        return  JSONObject.toJSONString(this, JSONWriter.Feature.WriteNulls);
    }
}
